﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Linq;
using DDU.DataModel;
using DDU.Library;
using System.Windows;

namespace DDU.ViewModel
{
    public class BookGalleryViewModel:MainViewModel
    {
        private string guid;
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
                if (!string.IsNullOrWhiteSpace(Guid))
                {
                    GetBookItems(Guid);
                    SemesterInfo = Database.CollectionOfSemesters.FirstOrDefault(s => s.Guid == Guid);
                }
            }
        }

        private ObservableCollection<BookInfo> itemsOfBooks;
        public ObservableCollection<BookInfo> ItemsOfBooks
        {
            get { return itemsOfBooks; }
            set
            {
                itemsOfBooks = value;
                RaisePropertyChanged(() => ItemsOfBooks);
            }
        }

        private SemesterInfo semesterInfo;
        public SemesterInfo SemesterInfo
        {
            get { return semesterInfo; }
            set
            {
                semesterInfo = value;
                RaisePropertyChanged(() => SemesterInfo);
            }
        }

        private BookInfo currentBookItem;
        public BookInfo CurrentBookItem
        {
            get { return currentBookItem; }
            set
            {
                currentBookItem = value;
                RaisePropertyChanged(() => CurrentBookItem);
            }
        }

        private Visibility slideVisibility = Visibility.Collapsed;
        public Visibility SlideVisibility
        {
            get { return slideVisibility; }
            set
            {
                slideVisibility = value;
                RaisePropertyChanged(() => SlideVisibility);
            }
        }

        private bool showDetails = true;
        public bool ShowDetails
        {
            get { return showDetails; }
            set
            {
                showDetails = value;
                RaisePropertyChanged(() => ShowDetails);
            }
        }

        private void GetBookItems(string semesterGuid)
        {
            IEnumerable<BookInfo> items = Database.CollectionOfBooks.Where(b => b.SemesterGuid == semesterGuid);
            ItemsOfBooks = new ObservableCollection<BookInfo>(items);
        }

        public ICommand PlayCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        SlideVisibility = Visibility.Visible;
                        SlidePlayEvent.Invoke(this, new SlidePlayArgs() { Playing=true });
                    }
                };
            }
        }

        public ICommand StopCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        SlideVisibility = Visibility.Collapsed;
                        SlidePlayEvent.Invoke(this, new SlidePlayArgs() { Playing = false });
                    }
                };
            }
        }

        public event EventHandler<SlidePlayArgs> SlidePlayEvent = delegate { };

    }

    public class SlidePlayArgs : EventArgs
    {
        public bool Playing { get; set; }
    }
}
